const app = getApp()
const utils = require('../../../utils/util.js')
import Toast from '../../../vant-weapp/dist/toast/toast'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    avatarUrl: '', // 头像 /static/img/icon_logo.png
    nickName: '', // 昵称
    phone: '', // 手机号
    gender: '', // 性别
    encryptedDataPhone: '', // 手机号
  },

  /**
   * 获取头像
   */
  onChooseAvatar(e) {
    console.log(e.detail.avatarUrl)
    const { avatarUrl } = e.detail
    const that = this
    // 将本地头像上传至远程服务器
    wx.uploadFile({
      url: app.globalData.api.getRealAttachmentUpload,
      filePath: avatarUrl,
      name: 'file',
      success(res) {
        const resData =  JSON.parse(res.data)
        if(resData.success === 1 && resData.data) {
          that.setData({ avatarUrl: utils.filterURL(resData.data.enclosurePath) })
        }
      }
    })
  },
  /**
   * 获取昵称
   */
  onChangeNickName(e) {
    this.setData({ nickName: e.detail.value })
  },
  handleDisagree(){
    wx.navigateBack({delta: 1})
  },
   /**
   * 获取昵称
   */
  genderChange(e) {
    this.setData({ gender: e.detail.value })
  },
  /**
   * 获取手机号
   */
  getPhoneNumber(e) {
    console.log(e)
    const { type = ''} = e.currentTarget.dataset
    const { encryptedData: encryptedDataPhone, errMsg, iv } = e.detail

    if (errMsg === 'getPhoneNumber:fail user deny' || errMsg === 'getPhoneNumber:fail:user deny'){
      Toast({
        position: 'top',
        message: '授权失败'
      });
      return false
    }
    let sessionKey = wx.getStorageSync('session_key')
    utils.$http(app.globalData.api.GetPhone, {
      encryptedDataPhone,
      iv,
      sessionKey,
    },"POST", false ,"JSON").then(res=> {
      console.log(res)
      if(res.success === 1) {
        this.setData({ encryptedDataPhone: res.msg })
      }
    })
  },
  /**
   * 注册
   */
  GetwxRegister() {
    const { avatarUrl, nickName, gender, encryptedDataPhone } = this.data
    if(!avatarUrl) {
      Toast({ position: 'top', message: '请获取头像' });
      return
    } 
    if(!nickName) {
      Toast({ position: 'top', message: '请完善昵称' });
      return
    }
    if(!gender) {
      Toast({ position: 'top', message: '请选择性别' });
      return
    }  
    if(!encryptedDataPhone) {
      Toast({ position: 'top', message: '请获取手机号' });
      return
    }  
    let headImg = avatarUrl
    let code = wx.getStorageSync('wechatcode')
    let openId = wx.getStorageSync('openId')
    // let sessionKey = wx.getStorageSync('session_key')
    let unionId = wx.getStorageSync('unionid')
    const postData = { code, encryptedDataPhone, nickName, gender,  headImg,  openId, unionId } 

    let pages = getCurrentPages(); // 当前页面
    let beforePage = pages[pages.length - 2]; // 上一页

    utils.$http(app.globalData.api.GetwxRegister, postData ,"POST", false ,"JSON").then(res=> {
      const { success , msg, data } = res
      if(success === 1) {
        let phone = data && data.phone
        wx.setStorageSync('userInfo', data) 
        wx.setStorageSync('phone', phone)
        wx.navigateBack({
          success: function() {
            beforePage.getLocalUserInfo(); // 上一页 重新获取本地信息
          }
        })
      } else {
        wx.showToast({ mask: true, icon: 'none', duration: 2000, title: '注册失败：电话号码已注册或用户微信信息不完善' });
      }
    })
  },
  /**
   * 确认提交
   */
  handleSumbit() {
    this.GetwxRegister()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})